package com.app.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.app.common.Result;
import com.app.model.InviteRecordModel;
import com.app.model.UserModel;
import com.app.service.InviteRecordService;
import com.app.service.UserService;
import com.app.utils.CrmebDateUtil;
import com.app.utils.DateLimitUtilVo;
import com.app.utils.TokenUtils;
import com.app.vo.InviteMonthCountVo;
import com.app.vo.InviteRecordVo;
import com.app.vo.UserVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 邀请记录
 *
 * @author 16693103420
 * @since 2024-02-03
 */
@RestController
@RequestMapping("/inviteRecord")
public class InviteRecordController {

    @Autowired
    private InviteRecordService inviteRecordService;
    @Autowired
    private UserService userService;


    @GetMapping("/getUserInviteRecordList")
    public Result getUserInviteRecordList(@RequestParam(defaultValue = "0") Integer page,
                                          @RequestParam(defaultValue = "10") Integer limit, HttpServletRequest request){
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        Integer userId = userModel.getId();
        IPage<UserModel> list = inviteRecordService.getUserInviteRecordList(page,limit,userId);
        return Result.success(list);
    }


    // 用户的推广
    @GetMapping("/getUserInviteRecord")
    public Result getUserInviteRecord(@RequestParam(defaultValue = "0") Integer page,
                                      @RequestParam(defaultValue = "10") Integer limit, String keyword, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel userModel = userService.getUserByOpenid(openId);
        if (userModel == null) {
            return Result.error(405, "用戶不存在");
        }
        Integer userId = userModel.getId();
        InviteRecordVo inviteRecordList = inviteRecordService.getUserInviteRecord(page, limit, userId, keyword);
        return Result.success(inviteRecordList);
    }

    //邀请好友
    @PostMapping("/inviteUser")
    public Result inviteUser(@RequestBody UserModel userModel, HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel user = userService.getUserByOpenid(openId);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        Integer quiltInviteId = user.getId();
        QueryWrapper<InviteRecordModel> quiltInviteQW = new QueryWrapper<>();
        quiltInviteQW.eq("quilt_invite_id", quiltInviteId);
        List<InviteRecordModel> quiltInviteList = inviteRecordService.list(quiltInviteQW);
        if (CollectionUtil.isNotEmpty(quiltInviteList)) {
            return Result.error(201, "此用户已被邀请过了");
        }
        String invitationCode = userModel.getInvitationCode();
        QueryWrapper<UserModel> inviteQW = new QueryWrapper<>();
        inviteQW.eq("invitation_code", invitationCode);
        UserModel invitePerson = userService.getOne(inviteQW);
        Integer inviteId = invitePerson.getId();
        if (inviteId.equals(quiltInviteId)) {
            return Result.error(201, "不可以邀请自己");
        }
//        inviteRecordService.inviteUser(invitationCode, quiltInviteId);
        return Result.success("邀请成功");
    }

    //查询本月邀请人数
    @GetMapping("/inviteUserMonthCount")
    public Result inviteUserMonthCount(HttpServletRequest request) {
        String token = request.getHeader("token");
        if (token == null) {
            return Result.error(405, "token不存在");
        }
        String openId = TokenUtils.verify(token);
        UserModel user = userService.getUserByOpenid(openId);
        if (user == null) {
            return Result.error(405, "用户不存在");
        }
        InviteMonthCountVo inviteMonthCountVo = new InviteMonthCountVo();
        QueryWrapper<InviteRecordModel> inviteRecordModelQueryWrapper = new QueryWrapper<>();
        inviteRecordModelQueryWrapper.eq("invite_id", user.getId());
        int count = inviteRecordService.count(inviteRecordModelQueryWrapper);
        inviteRecordModelQueryWrapper.eq("status", 2);
        int count1 = inviteRecordService.count(inviteRecordModelQueryWrapper);
        inviteMonthCountVo.setCount(count1);
        inviteMonthCountVo.setMaxCount(count);
        return Result.success(inviteMonthCountVo);
    }

    @GetMapping("/userList")
    public Result userList(Integer page, Integer limit, Integer id, String beginTime,String endTime,String name) throws Exception {

        IPage<UserModel> inviteRecordModelPage = inviteRecordService.queryLoginUserInvitationRecord( page,  limit,  id, beginTime, endTime, name);
        VO userVVO = new VO();
        userVVO.setCount(inviteRecordModelPage.getTotal());
        userVVO.setList(inviteRecordModelPage.getRecords());
        return Result.success(userVVO);


    }

}


@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
class VO {
    //该用户券的记录集合
    private List<UserModel> list;
    private Long count;

}

